#include "synch.h"

// The following class defines a "bounded buffer" -- which is
// used to give a solution of a classical synchronization
// problem called bounded producer/consumer. 
class BoundedBuffer {
   public:
     // create a bounded buffer with a limit of 'maxsize' bytes
     BoundedBuffer(int maxsize);

     ~BoundedBuffer();
     
     // read 'size' bytes from the bounded buffer, storing into 'data'.
     // ('size' may be greater than 'maxsize')
     void Read(void *data, int size);
     
     // write 'size' bytes from 'data' into the bounded buffer.
     // ('size' may be greater than 'maxsize')
     void Write(void *data, int size);
   private:

   	void *buffer; // the bounded buffer for data read and write
   	int max_size; // the max size of buffer
   	int fill_ptr; // the position of write process in buffer
   	int use_ptr;  // the position of read process in buffer
   	int count;  // the fact bytes of the buffer

   	Lock *mutex;  // ensure exclusive access
   	Condition *empty; // ensure the conditional synchronization
                      // condition empty for the read threads
   	Condition *fill;  // condition fill for the write threads
};

